package at.ac.tuwien.ifs.calendit.basis.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.ITODO;
import at.ac.tuwien.ifs.calendit.domain.TODO;


/**
 * 
 * The Table model encapsulates the complete data needed for the table in the
 * GUI form. This includes reading the data from the database using the data
 * access object (DAO).
 * 
 * @author The SE-Team
 * @version 1.0
 * 
 */

public class TODOTableModel extends AbstractTableModel {
	
		/**
	 	* serialVersionUID, generated by eclipse.
	 	*/
		private static final long serialVersionUID = 1L;

		/**
		 * Retrieves the logger for this class.
		 */
		private static Log log = LogFactory.getLog(TODOTableModel.class);

		/**
		 * The Data Access Object for todo.
		 */
		private ITODO todoDAO= null;

		/**
		 * Spring Framework XML Bean Factory.
		 */
		private XmlBeanFactory xbf;

		/**
		 * Defines the Names of the Columns of the TableModel.
		 */
		private final String[] columnName = {"Bezeichnung", "Deadline Datum", "Deadline Zeit"};

		/**
		 * The list of toodo will be filled by TodoDAO.
		 */
		private List<TODO> todo = null;

		/**
		 * Order of the TableModel, must be "Deadline".
		 */
		private String order = "";

		/**
		 * Creates a new TodoTableModel with default ordering.
		 * 
		 * @param order
		 *            the default Order
		 * @see #order
		 */
		public TODOTableModel(String order) {
			super();
			ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS);
			xbf = new XmlBeanFactory(res);
			todoDAO = (ITODO) xbf.getBean("todoDAO");
			this.order = order;
			readData();
		}

		/**
		 * Get a List of todo represented in this TableModel.
		 * 
		 * @return the todo represented in the TableModel
		 * @see Todo
		 * @see List
		 */
		public List<TODO> getTODO() {
			return todo;
		}

		/**
		 * Returns a single todo out of this TableModel.
		 * 
		 * @param i
		 *            The index of the desired todo.
		 * @return The todo at position <c>i</c>.
		 * @see #getTODO()
		 */
		public TODO getTODOAt(int i) {
			return todo.get(i);
		}

		/**
		 * Read the todo from the Database with given order.
		 * 
		 * @see TODODAO#getTODO(String)
		 */
		private void readData() {
			todo = todoDAO.getTODO(order);
		}

		/**
		 * Reload the Data.
		 */
		public void reload() {
			readData();
			fireTableDataChanged();
		}

		/**
		 * Change the Order and reload the Data.
		 * 
		 * @param order
		 *            the new Order
		 * @see #order
		 */
		public void setOrder(String order) {
			log.info("Order Changed: reading data...");
			this.order = order;
			reload();
		}

		/**
		 * Get the number of rows in this TableModel.
		 * 
		 * @return number of rows
		 */
		public int getRowCount() {
			if (todo != null) {
				log.debug("rowcount = " + todo.size());
				return todo.size();
			} else return 0;
			
		}

		/**
		 * Get the number of columns in this TableModel.
		 * 
		 * @return number of colums
		 */
		public int getColumnCount() {
			log.debug("colCount = " + columnName.length);
			return columnName.length;
		}

		/**
		 * Get the title of a column.
		 * 
		 * @param c
		 *            the column-index
		 * @return the title
		 */
		public String getColumnName(int c) {
			log.debug("colName = " + columnName[c]);
			return columnName[c];
		}

		/**
		 * Get the Class of the data saved in a colum.
		 * 
		 * @param c
		 *            the column-index
		 * @return String.class
		 */
		public Class<?> getColumnClass(int c) {
			return String.class;
		}

		/**
		 * Checks whether a cell is editable or not.
		 * 
		 * @param row
		 *            the Row
		 * @param col
		 *            the Column
		 * @return false in any case.
		 */
		public boolean isCellEditable(int row, int col) {
			return false;
		}

		/**
		 * Get the Value from a given Cell, denoted by row and col.
		 * 
		 * @param row
		 *            the Row
		 * @param col
		 *            the Column
		 * @return String containing the value of the cell.
		 */
		public Object getValueAt(int row, int col) {
			log.debug("getValue row = " + row + " col = " + col);
			TODO t = todo.get(row);
			switch (col) {
			case 0:
				return "" + t.getBezeichnung();
			case 1:
				return t.getDeadlineDatum();
			case 2:
				return t.getDeadlineTime();
			
			default:
				return null;
			}
		}
		
		/**
		 * 
		 */
		
		public List<TODO> getHeute() {
			return todoDAO.getTODOHeute();
		}
		public List<TODO> get7Tage(){
			return todoDAO.get7Tage();
		}
		public List<TODO>getAll(){
			return todoDAO.getAll();
		}
		public List<TODO>getAbgeschlossene(){
			return todoDAO.getAbgeschlossen();
		}
		public List<TODO>getOffene(){
			return todoDAO.getOffene();
		}
		public List<TODO>getHeuteAbgeschlossen(){
			
			
			return todoDAO.getHeuteAbgeschlossen();
		}
		public List<TODO>getHeuteOffene(){
			return todoDAO.getHeuteOffene();
		}
		
		public int add(TODO todo){
		
			TODO todotemp=todoDAO.add(todo);
				return todotemp.getAufgabeId();
		}
		
		public TODO getTodoByID(int id){
			return todoDAO.getById(id);
		}
		
		
		 public void changeContent(){
	        	if(TODOVerwaltungFrame.rb_Alle.isSelected())
	        		if(TODOVerwaltungFrame.cb_abgeschlossen.isSelected())
	        			if(TODOVerwaltungFrame.cb_offene.isSelected())
	        				todo=getAll();
	        			else
	        				todo= getAbgeschlossene();
	        		else
	        			todo= getOffene();
	        	else
	        		if(TODOVerwaltungFrame.rb_Heute.isSelected())
		        		if(TODOVerwaltungFrame.cb_abgeschlossen.isSelected())
	            			if(TODOVerwaltungFrame.cb_offene.isSelected())
	            				todo=getHeute();
	            			else
		        				todo= getHeuteAbgeschlossen();
	            		else
		        			todo= getHeuteOffene();
	        		else
		        		if(TODOVerwaltungFrame.cb_abgeschlossen.isSelected())
	            			if(TODOVerwaltungFrame.cb_offene.isSelected())
	            				todo=getAll();
	            			else
		        				todo= getAbgeschlossene();
	            		else
		        			todo= getOffene();
	        	
				
				fireTableDataChanged();
			}
	
		 public boolean deleteTODO(int id){
			 return todoDAO.delete(id);
		 }
		 
		 public boolean deleteByKennung(int id){
			 return todoDAO.deleteByKennung(id);
		 }
		 
		 public boolean deleteWith2Arguments(TODO t){
			 return todoDAO.deleteWith2Arguments(t);  
		} 
		 
		 public boolean done(int aufgabeId){
			 return todoDAO.done(aufgabeId);
		 }
		 
}
